﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using BIExtAuth.Model;
using BIExtAuth.BLL;
using BIExtAuth.Common;

namespace BIExtAuth.Web.Admin
{
    public partial class CreateMember : System.Web.UI.Page
    {
        UserBLL userBLL = new UserBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

            }
        }

        protected void BtnSave_Click(object sender, EventArgs e)
        {
            BIUser user = this.userBLL.GetUserByUserID(this.txtUserID.Text.Trim());
            if (user == null)
            {
                BIUser adminUser = this.userBLL.GetUserByUserID(this.txtAdminUser.Text.Trim());
                if (adminUser.UserID == "biadmin" && adminUser.UserPwd == BIExtAuth.Common.SecurityEncrypt.DesEncrypt(this.txtAdminPwd.Text))
                {
                    user = new BIUser();

                    user.UserID = txtUserID.Text.Trim();
                    user.UserName = txtFullName.Text.Trim();
                    user.UserDesc = txtDesc.Text.Trim();
                    user.UserEmail = txtEmail.Text.Trim();
                    user.UserPwd = SecurityEncrypt.DesEncrypt(txtPwd.Text);
                    user.UserStatus = ddlState.SelectedValue == "1" ? true : false;

                    try
                    {
                        int ret = this.userBLL.AddUser(user);
                        if (ret > 0)
                        {
                            msg.Text = "创建用户成功！";
                            this.txtUserID.Text = "";
                            this.txtFullName.Text = "";
                            this.txtDesc.Text = "";
                            this.txtEmail.Text = "";
                            this.ddlState.SelectedIndex = 0;
                            this.txtAdminUser.Text = "";
                            this.txtUserID.Focus();
                        }
                        else
                        {
                            msg.Text = "创建用户失败！";
                        }
                    }
                    catch (Exception ex)
                    {
                        msg.Text = "系统错误。创建用户失败！\n" + ex.Message;
                    }
                }
                else
                {
                    msg.Text = "管理员账号或密码错误！";
                }
            }
            else
            {
                msg.Text = "用户ID已经存在！";
            }
        }
    }
}